// pages/webview/webview.js
const QQMapWX = require('../../lib/qqmap-wx-jssdk.min.js')

var qqmapsdk;
const app = getApp()

Page({

  /**
   * 页面的初始数据f
   */
  data: {
    url:"",
    imageUrl:"../../image/weixin.png"
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {  
    var that = this;
    qqmapsdk = new QQMapWX({
      key: 'BYXBZ-763KX-YTT4S-7SEOC-N5I23-I5BAE'
    });

    //刚刚初始化进来
    if("{}"==JSON.stringify(options)){
      that.getAddress();
      // setTimeout(function(){
      //   if(wx.getStorageSync('city')== ""){
      //     that.setData({
      //       url:"https://homeback.obs.cn-east-3.myhuaweicloud.com/home.html?openid="+ wx.getStorageSync('openid')
      //     });
      //   }else{
      //     that.setData({
      //       url:"https://homeback.obs.cn-east-3.myhuaweicloud.com/home.html?city="+wx.getStorageSync('city')+"&openid="+wx.getStorageSync('openid')
      //     });
      //   }       
      // },1000);      
    }
   
    if(options.type == "phone"){     
      if(options.path=="chat"){      
        that.setData({
          //url:"https://homeback.obs.cn-east-3.myhuaweicloud.com/chat_list.html?telephone="+options.telphone
          url:"https://homeback.yzqysoft.com/"+app.globalData.AppID+"/chat_list.html?telephone="+options.telphone
        });
      }else{
        that.setData({
          //url:"https://homeback.obs.cn-east-3.myhuaweicloud.com/my.html?telephone="+options.telphone
          url:"https://homeback.yzqysoft.com/"+app.globalData.AppID+"/my.html?telephone="+options.telphone
        });
      }      
    }

    if(options.type == "userinfo"){     
      that.setData({
        //url:"https://homeback.obs.cn-east-3.myhuaweicloud.com/my.html?nickname="+options.nickname+"&avatarUrl="+options.avatarUrl+"&openid="+ app.globalData.openid
        url:"https://homeback.yzqysoft.com/"+app.globalData.AppID+"/my.html?nickname="+options.nickname+"&avatarUrl="+options.avatarUrl+"&openid="+ app.globalData.openid
      })
    }   
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  // 接收web-view传来的数据
  getmessage(e) {      
    let obj = e.detail.data[e.detail.data.length - 1];
    this.setData({
      message:obj
    })
  },
  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {
    let that = this

    return {
      title: that.data.message.title,
      path: '/pages/share/share?url=' + encodeURIComponent(that.data.message.url),
    
      imageUrl: that.data.imageUrl,
      success: function (res) {
        console.log("转发成功")
        // 转发成功
      },
      fail: function (res) {
          console.log("转发失败")
          // 转发失败
      }
    }
  },

  getAddress:function () {
    var that = this
    wx.authorize({
      scope: 'scope.userLocation',//发起定位授权
      success: function () {
        // console.log('有定位授权')
        // wx.getLocation({
        //   type: 'wgs84',
        //   success (res) {
        //     const latitude = res.latitude
        //     console.log(latitude);
        //     const longitude = res.longitude
        //     console.log(longitude)
        //     const speed = res.speed
        //     const accuracy = res.accuracy
        //   }
        //  })

        that.getCity();
        
      //授权成功，此处调用获取定位函数
      }, fail() {
        //如果用户拒绝授权，则要告诉用户不授权就不能使用，引导用户前往设置页面。
        //console.log('没有定位授权')
        wx.showModal({
          cancelColor: 'cancelColor',
          title: '没有授权无法获取位置信息',
          content: '是否前往设置页面手动开启',
          success: function (res) {
            if (res.confirm) {
              wx.openSetting({
                withSubscriptions: true,
              })
            } else {
              wx.showToast({
                icon: 'none',
                title: '您取消了定位授权',
              })
            }
          }, fail: function (e) {
            console.log(e)
          }
        })
      }
    })
  },

  getCity:function () {
    var that = this
    wx.getLocation({//获取地址
       type: 'gcj02',
       success(res) {
         const latitude = res.latitude
         const longitude = res.longitude
         const speed = res.speed
         const accuracy = res.accuracy
         qqmapsdk.reverseGeocoder({//SDK调用
           location: { latitude, longitude },
           success: function (res) {
             var cur_city =  res.result.address_component.city
             if(cur_city!=""){
              app.globalData.city = cur_city;
              //重新加载webview的定向首页
              that.redirect(cur_city);
             }
           },
           fail:function(err){
             console.log(err, "getCity error");
           }
         })
       }
     })
   },
   redirect:function(city){     
     var that = this
     wx.setStorageSync('city', city);
     that.setData({
       //url:"https://homeback.obs.cn-east-3.myhuaweicloud.com/home.html?city="+city+"&openid="+wx.getStorageSync('openid')+"&appid="////+app.globalData.AppID+"&role=customer",
       url:"https://homeback.yzqysoft.com/"+app.globalData.AppID+"/home.html?city="+city+"&openid="+wx.getStorageSync('openid')+"&appid="+app.globalData.AppID+"&role=customer"

       //url:"https://homeback.obs.cn-east-3.myhuaweicloud.com/home.html?openid="+wx.getStorageSync('openid')+"&city="+city
     })
   }
})

